import { CHANGE_PLAYING_MUSIC_SRC, CHANGE_PLAYING_MUSIC_PLAN, CHANGE_PLAYING_MUSIC_LYRIC,
  CHANGE_PLAYING_MUSIC_AFTER, CHANGE_PLAYING_MUSIC_LATER
} from '../types/common'
import Request from '@/api/request'

const changePlayingMusicAfter = (data) => dispatch => dispatch(
  {
    data,
    type: CHANGE_PLAYING_MUSIC_AFTER
  }
)

const changePlayingMusicLater = (data) => dispatch => dispatch(
  {
    data,
    type: CHANGE_PLAYING_MUSIC_LATER
  }
)

const changePlayingMusicSrc = (data) => {
  return dispatch => {
    getLyric(data.id).then(res => {
      const { lrc } = res
      return dispatch({
        data: {...data, ...lrc },
        type: CHANGE_PLAYING_MUSIC_SRC
      })
    })
  }
}

const changePlayingMusicLyric = (data) => dispatch => {
  getLyric().then(res => {
    const { lrc } = res
    return dispatch((
      {
        data: { ...data, ...lrc },
        type: CHANGE_PLAYING_MUSIC_LYRIC
      }
    ))
  })
}

const changePlayingMusicPlan = (data) => ({
  data,
  type: CHANGE_PLAYING_MUSIC_PLAN
})


const getLyric = (id = '33419836') => {
  return Request.get(`/cloudmusic/lyric?id=${id}`)
}

export {
  changePlayingMusicSrc, changePlayingMusicPlan, changePlayingMusicLyric, getLyric, changePlayingMusicAfter, changePlayingMusicLater
}